﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string username = ".jpg.png.gif";
            string name = FileUpload1.FileName;
            string exname = name.Substring(name.LastIndexOf('.'));
            if (username.Contains(exname)==false){
                Response.Write("<script>alert('不支持文件格式！')</script>");
                return;
            }
            if (FileUpload1.FileContent.Length > 1 * 1024 * 1024)
            {
                Response.Write("<script>alert('不能上传超过1M的图片！')</script>");
            }
            //1.获取文件名称
            //2.设置文件保存路径
            string path =Server.MapPath("img")+"\\"+name;
            //3.调用空间执行文件保存
            if (name != "")
            {
                FileUpload1.SaveAs(path);
            }
            //4.给Image组件夫路径，让他显示图片
            Image1.ImageUrl = "img/" + name;//虚拟地址
        }
    }
}